package com.yanqu.road.server.cmd.activity.unionwar;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.pb.activity.unionwar.UnionWarActivityServerProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.unionwar.UnionWarActivityMgr;
import com.yanqu.road.server.manger.activity.unionwar.UnionPhaseWarMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.manger.union.GameUnionActivityGroupMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.string.StringUtils;

import java.util.List;

@Cmd(code = Protocol.S_UNION_WAR_USER_PATRONS_DISPATCH,desc = "备战-派遣门客")
public class UnionWarUserDispatchPatronsCmd  extends AbstractCommand {

    private int activityId;
    private int phase;
    private int session;
    private List<Integer> patronsIdList;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        UnionWarActivityServerProto.UserDispatchReqMsg reqMsg = UnionWarActivityServerProto.UserDispatchReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        phase = reqMsg.getPhase();
        session = reqMsg.getSession();
        patronsIdList = reqMsg.getPatronsIdList();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if(patronsIdList.size() == 0){
            ret = GameErrorCode.E_UNION_WAR_ACTIVITY_NO_APPOINT_PATRONS;
            return;
        }
        if(patronsIdList.size() > 2){
            ret = GameErrorCode.E_REQUEST_ARGUMENT_ERROR;
            return;
        }
        if(patronsIdList.size() == 2 && patronsIdList.get(0).intValue() == patronsIdList.get(1).intValue()){
            patronsIdList.remove(1);
        }
        if(patronsIdList.get(0) == 0){
            ret = GameErrorCode.E_UNION_WAR_ACTIVITY_NO_APPOINT_PATRONS;
            return;
        }
        ActivityInfo activityInfo = UnionWarActivityMgr.getOpenActivityInfo(activityId);
        if(activityInfo == null){
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }
        String unionUid = GameUnionActivityGroupMgr.getUnionUid(activityId, player.getUserId());
        if(StringUtils.isNullOrEmpty(unionUid)){
            ret = GameErrorCode.E_UNION_NO_JOIN_UNION;
            return;
        }
        ret = UnionPhaseWarMgr.dispatchPatrons(player, activityId, unionUid, phase, session, patronsIdList);
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            UnionWarActivityServerProto.UserDispatchRespMsg.Builder respMsg = UnionWarActivityServerProto.UserDispatchRespMsg.newBuilder();
            respMsg.setRet(ret);
            respMsg.setPhase(phase);
            respMsg.setSession(session);
            player.sendPacket(Protocol.U_UNION_WAR_USER_PATRONS_DISPATCH, respMsg);
        }
    }
}
